package logic

import (
	"go.uber.org/zap"
	"personal-blog/dao/mysql"
	"personal-blog/models"
	"personal-blog/pkg/snowflake"
)

//创建博客
func CreateBlog(blog *models.Blog) (err error) {
	// 生成帖子ID
	blogID, err := snowflake.GetID()
	if err != nil {
		zap.L().Error("snowflake.GetID() failed", zap.Error(err))
		return
	}
	blog.BlogID = blogID
	// 创建帖子
	if err := mysql.CreatePost(blog); err != nil {
		zap.L().Error("mysql.CreatePost(&post) failed", zap.Error(err))
		return err
	}

	return

}

//得到用户列表
func GetUserDetail(username string) ([]models.Blog, error) {
	return mysql.GetUserDetailByUserName(username)
}

//获取博客详情
func GetBlogDetail(id int64) (*models.Blog, error) {
	return mysql.GetBlogDetailByID(id)
}

//修改博客
func UpdateBlogDetail(id int64, blog *models.Blog) error {
	return mysql.UpdateBlogDetailByID(id, blog)
}

func GetBlogList(page, size int) (data []*models.Blog, err error) {
	//查询并组合我们接口想用的数据
	blogs, err := mysql.GetBlogList(page, size)
	if err != nil {
		zap.L().Error("mysql.GetPostList(page,size) failed", zap.Error(err))
		return
	}
	data = blogs

	return
}

//删除博客
func DeleteBlogDetail(id int64) error {
	return mysql.DeleteBlogDetailByID(id)
}
